<?php
namespace app\api\controller;
use app\common\controller\Frontend;
use app\common\model\User;
use app\common\model\Account;
use app\common\model\SmsCode;
use think\Session;
use think\File;
use think\Db;
class Common extends Frontend{
    public function upload(){
        $file = $this->request->file('file');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'storage');
        if($info){
            $fileInfo = $file->getInfo();
            $fileData['storage'] = DS.'public' . DS . 'storage';
            $fileData['source_name'] = $fileInfo['name'];
            $fileData['mini_type'] = $fileInfo['type'];
            $fileData['file_size'] = $fileInfo['size'];
            $fileData['file_extension'] = $info->getExtension();
            $fileData['file_name'] = $info->getFilename();
            $fileData['url'] = DS . 'storage'. DS.$info->getSaveName();
            model('FileAttachments')->save($fileData);
            $this->success('',$fileData['url'],200);
        }else{
            $this->error('',$file->getError());
        }
    }

    public function send(){
        if($this->request->isPost()){
            $mobile = input('mobile','');
            $verify = input('is_verify','0');
            
            if(!preg_match('/^1[3456789]\d{9}$/', (string) $mobile)){
                $this->error('手机号格式错误');
            }
            if($verify=='1'){
                $user = User::where('mobile',$mobile)->find();
                if(empty($user)){
                    $this->result(null,300,'此用户不存在，请先注册');
                }
            }
            
            $code = rand(1000,9999);
            $sms = new SmsCode;
            
            Db::startTrans();
            try {
                $sms->mobile = $mobile;
                $sms->verify = $code;
                $sms->save();
                /**
                 * 发送验证码
                 */
                Db::commit();
                $this->success('发送成功');
            } catch (\think\Exception $e) {
                Db::rollback();
                $this->error('发送失败');
            }
        }
    }

    public function login(){
        if($this->request->isPost()){
            $mobile = input('mobile','');
            $verify = input('verify','');
            if(!preg_match('/^1[3456789]\d{9}$/', (string) $mobile)){
                $this->error('手机号格式错误');
            }
            if(empty($verify)){
                $this->error('请输入验证码');
            }
            $sms = SmsCode::where('mobile',$mobile)->where('verify',$verify)->find();
            if(empty($sms) || $sms['status'] != '0'){
                SmsCode::where('mobile',$mobile)->setField('status','1');
                $this->error('验证码错误');
            }else{
                SmsCode::where('mobile',$mobile)->setField('status','1');
            }
            $login = $this->auth->loginMobile($mobile);
            if($login){
                $this->success('登录成功');
                
            }else{
                $this->error($this->auth->getError());
            }
        }
    }

    public function register(){
        if($this->request->isPost()){
            $mobile = input('mobile','');
            $verify = input('verify','');
            if(!preg_match('/^1[3456789]\d{9}$/', (string) $mobile)){
                $this->error('手机号格式错误');
            }
            if(empty($verify)){
                $this->error('请输入验证码');
            }
            $sms = SmsCode::where('mobile',$mobile)->where('verify',$verify)->find();
            if(empty($sms) || $sms['status'] != '0'){
                SmsCode::where('mobile',$mobile)->setField('status','1');
                $this->error('验证码错误');
            }else{
                SmsCode::where('mobile',$mobile)->setField('status','1');
            }
            $old = User::getByMobile($mobile);
            if(!empty($old)){
                $this->error('此手机号已经绑定了用户，请联系管理员处理');
            }
            $openid = Session::get('lifeOpenid');
            if(empty($openid)){
                $this->error('数据错误,请稍后重试');
            }
            $account = Account::where('account_phone',$mobile)->find();
            $user =  User::where('openid',$openid)->find();
            if(!empty($account)){
               $user->mobile = $mobile;
               $user->account_id = $account->id;
               $user->is_verify = 1;
               $user->save();
            }else{
                $account = new Account;
                $account->account_phone = $mobile;
                $account->account_card = '99900'.$mobile;
                $account->balance = 0;
                $account->extra_amount = 0;
                $account->save();
                $user->mobile = $mobile;
                $user->account_id = $account->id;
                $user->is_verify = 1;
                $user->save();
            }
            $this->success('注册成功');
        }
    }
}